package com.xxh.securitydemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxh.securitydemo.dao.UmsRoleDao;
import com.xxh.securitydemo.model.UmsRole;
import com.xxh.securitydemo.model.UmsRoleMenuRelation;
import com.xxh.securitydemo.service.UmsRoleMenuRelationService;
import com.xxh.securitydemo.service.UmsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 后台用户角色表(UmsRole)表服务实现类
 *
 * @author makejava
 * @since 2020-10-29 08:42:44
 */
@Service("umsRoleService")
public class UmsRoleServiceImpl extends ServiceImpl<UmsRoleDao, UmsRole> implements UmsRoleService {


    @Autowired
    UmsRoleMenuRelationService umsRoleMenuRelationService;

    @Override
    public boolean allocRoleMenus(Long rid, List<Long> menuIds) {
        //构造插入的bean
        List<UmsRoleMenuRelation>lists=new ArrayList<>();
        for (int i=0;i<menuIds.size();i++){
            UmsRoleMenuRelation relation = new UmsRoleMenuRelation();
            relation.setRoleId(rid);
            relation.setMenuId(menuIds.get(i));
            lists.add(relation);
        }
        //删除角色原来的菜单权限
        QueryWrapper<UmsRoleMenuRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",rid);
        umsRoleMenuRelationService.remove(wrapper);
        //录入新的权限
        return umsRoleMenuRelationService.saveBatch(lists);

    }
}